/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.common.service.base.event;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.Component;
import org.noear.solon.core.event.EventListener;

/**
 * <b>(OrderLogEventListener)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2023/6/22
 */
@Slf4j
@Component
public class OrderLogEventListener implements EventListener<ServiceOrderLogEvent> {

    @Override
    public void onEvent(ServiceOrderLogEvent event) {
        if (ObjectUtil.isNotNull(event.getOrder())) {
            String returnJson = JSON.toJSONString(event.getOrder());
            int    length     = 2_000;
            if (returnJson.length() > length) {
                log.info("json过长被截取");
                returnJson = returnJson.substring(0, 2000);
            }
            log.info("[{}]请求开始：Order=\n{}.", StrUtil.isNotEmpty(event.getOrder().getProductCode()) ? event
                            .getOrder().getProductCode() : event.getOrder().getClass().getSimpleName(),
                    returnJson);
        } else {
            log.warn("请求开始，居然没有填Order，不可思议亦不该出现.");
        }
    }
}
